package tools;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;

public class PackageUtils {
	private Context context;
	private PackageManager packageManager;
	private PackageInfo packageInfo;

	public PackageUtils(Context context) {
		this.context = context;
		init();
	}

	// 初始化数据
	public void init() {
		packageManager = context.getPackageManager();
		try {
			packageInfo = packageManager.getPackageInfo(
					context.getPackageName(), PackageManager.GET_ACTIVITIES);
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @return
	 */
	public int getVersionCode() {
		return packageInfo.versionCode;
	}

	/**
	 * 
	 * @return
	 */
	public String getVersionName() {
		return packageInfo.versionName;
	}

	/**
	 * 
	 * @param oldVersion
	 * @param newVersion
	 * @return
	 */
	public boolean isUpgradeVersion(int oldVersion, int newVersion) {
		boolean flag = false;
		flag = newVersion > oldVersion ? true : false;
		return flag;
	}

	/**
	 * 
	 * @param oldVersion
	 * @param newVersion
	 * @return
	 */
	public boolean isUpgradeVersionName(String oldVersion, String newVersion) {
		boolean flag = false;
		// 不相等即是有新版本
		flag = oldVersion.equals(newVersion) ? false : true;
		return flag;
	}
}
